//
//  ViewController.m
//  Test
//
//  Created by Housir on 2018/2/5.
//  Copyright © 2018年 侯玉坤. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    CGPoint center = self.view.center;
    CGFloat radius = (self.view.frame.size.width - 100)/2;
    NSDictionary *dic = [self calculateFivePointedStarTenPointWithCenter:center andRadius:radius];
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    basicAnimation.fromValue = @0.0;
    basicAnimation.toValue = @1.0;
    basicAnimation.duration = 10;
    CABasicAnimation *basicAnimation2 = [CABasicAnimation animationWithKeyPath:@"strokeStart"];
    basicAnimation2.fromValue = @0.0;
    basicAnimation2.toValue = @1.0;
    basicAnimation2.duration = 10;
    basicAnimation2.beginTime = 10;
    CAAnimationGroup *groupAnimation = [[CAAnimationGroup alloc]init];
    groupAnimation.animations = @[basicAnimation,basicAnimation2];
    groupAnimation.duration = 20.0;
    groupAnimation.repeatCount = MAXFLOAT;
    
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    NSArray *outerPoints = dic[@"outer"];
    NSArray *interiorPoints = dic[@"interior"];
    CGPoint outerP0 = [outerPoints[0][@"outerP"] CGPointValue];
    CGPoint outerP1 = [outerPoints[1][@"outerP"] CGPointValue];
    CGPoint outerP2 = [outerPoints[2][@"outerP"] CGPointValue];
    CGPoint outerP3 = [outerPoints[3][@"outerP"] CGPointValue];
    CGPoint outerP4 = [outerPoints[4][@"outerP"] CGPointValue];
    CGPoint interiorP0 = [interiorPoints[0][@"interiorP"] CGPointValue];
    CGPoint interiorP1 = [interiorPoints[1][@"interiorP"] CGPointValue];
    CGPoint interiorP2 = [interiorPoints[2][@"interiorP"] CGPointValue];
    CGPoint interiorP3 = [interiorPoints[3][@"interiorP"] CGPointValue];
    CGPoint interiorP4 = [interiorPoints[4][@"interiorP"] CGPointValue];
    [path moveToPoint:outerP0];
    [path addLineToPoint:interiorP3];
    [path addLineToPoint:interiorP4];
    [path addLineToPoint:outerP2];
    [path addLineToPoint:interiorP0];
    [path addLineToPoint:interiorP1];
    [path addLineToPoint:outerP4];
    [path addLineToPoint:interiorP2];
    [path addLineToPoint:interiorP3];
    [path addLineToPoint:outerP1];
    [path addLineToPoint:interiorP4];
    [path addLineToPoint:interiorP0];
    [path addLineToPoint:outerP3];
    [path addLineToPoint:interiorP1];
    [path addLineToPoint:interiorP2];
    [path closePath];
    
    CAShapeLayer *layer = [CAShapeLayer layer];
    layer.path = path.CGPath;
    layer.fillColor = [UIColor clearColor].CGColor;
    layer.strokeColor = [UIColor redColor].CGColor;
    [layer addAnimation:groupAnimation forKey:nil];
    [self.view.layer addSublayer:layer];
}


- (NSDictionary *)calculateFivePointedStarTenPointWithCenter:(CGPoint)center andRadius:(CGFloat)radius
{
    //按顺时针,第一个点在正上方
    //外层五个点
    CGPoint outerP0 = CGPointMake(center.x, center.y-radius);
    CGPoint outerP1 = CGPointMake(center.x+cos(18.0/180.0*M_PI)*radius, center.y-sin(18.0/180.0*M_PI)*radius);
    CGPoint outerP2 =CGPointMake(center.x+cos(54.0/180.0*M_PI)*radius, center.y+sin(54.0/180.0*M_PI)*radius);
    CGPoint outerP3 = CGPointMake(center.x-cos(54.0/180.0*M_PI)*radius, center.y+sin(54.0/180.0*M_PI)*radius);
    CGPoint outerP4 = CGPointMake(center.x-cos(18.0/180.0*M_PI)*radius, center.y-sin(18.0/180.0*M_PI)*radius);
    
    //内层半径
    CGFloat interiorRadius = (1 + pow(tan(18.0/180.0*M_PI), 2))/(3- pow(tan(18.0/180.0*M_PI), 2)) * radius;

    //内层五个点
    CGPoint interiorP0 = CGPointMake(center.x, center.y+interiorRadius);
    CGPoint interiorP1 = CGPointMake(center.x-cos(18.0/180.0*M_PI)*interiorRadius, center.y+sin(18.0/180.0*M_PI)*interiorRadius);
    CGPoint interiorP2 =CGPointMake(center.x-cos(54.0/180.0*M_PI)*interiorRadius, center.y-sin(54.0/180.0*M_PI)*interiorRadius);
    CGPoint interiorP3 = CGPointMake(center.x+cos(54.0/180.0*M_PI)*interiorRadius, center.y-sin(54.0/180.0*M_PI)*interiorRadius);
    CGPoint interiorP4 = CGPointMake(center.x+cos(18.0/180.0*M_PI)*interiorRadius, center.y+sin(18.0/180.0*M_PI)*interiorRadius);
    
    
    
    NSMutableDictionary *muDic = [[NSMutableDictionary alloc]init];
    NSArray *outer = @[
                      @{@"outerP":[NSValue valueWithCGPoint:outerP0]},
                      @{@"outerP":[NSValue valueWithCGPoint:outerP1]},
                      @{@"outerP":[NSValue valueWithCGPoint:outerP2]},
                      @{@"outerP":[NSValue valueWithCGPoint:outerP3]},
                      @{@"outerP":[NSValue valueWithCGPoint:outerP4]}
                      ];
    [muDic setObject:outer forKey:@"outer"];
    
    NSArray *interior = @[
                       @{@"interiorP":[NSValue valueWithCGPoint:interiorP0]},
                       @{@"interiorP":[NSValue valueWithCGPoint:interiorP1]},
                       @{@"interiorP":[NSValue valueWithCGPoint:interiorP2]},
                       @{@"interiorP":[NSValue valueWithCGPoint:interiorP3]},
                       @{@"interiorP":[NSValue valueWithCGPoint:interiorP4]}
                       ];
    [muDic setObject:interior forKey:@"interior"];
    
    return muDic;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
